﻿using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveProductHandbookProductDetailByProductIdServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveProductHandbookProductDetailByProductIdRequest, RetrieveProductHandbookProductDetailByProductIdResponse>
    {
        #region Properties
        public string Dao;
        public string Lang;
        public int ProductId;
        #endregion
        public RetrieveProductHandbookProductDetailByProductIdServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveProductHandbookProductDetailByProductIdRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.ProductId == 0)
            {
                throw new ArgumentException("Product ID must be set", "request");
            }
            else
            {
                ProductId = request.ProductId;
            }
            if (string.IsNullOrEmpty(request.Lang))
            {
                Lang = "en";
            }
            else
            {
                Lang = request.Lang;
            }
        }

        protected override bool OnExecute(RetrieveProductHandbookProductDetailByProductIdResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            ProductHandbookProductDetailRecord record = new ProductHandbookProductDetailRecord();
            ProductHandbookProductDetail listProductItem = Providers.ProductHandbooks.RetrieveProductHandbookProductDetail(ProductId);

            record = new ProductHandbookProductDetailRecord
            {
                ProductName = Lang.Equals("en") ? listProductItem.ProductNameEn : listProductItem.ProductName,
                Overview = Lang.Equals("en") ? listProductItem.OverviewProductEn : listProductItem.OverviewProduct,
                Document = Lang.Equals("en") ? listProductItem.DocumentProductEn : listProductItem.DocumentProduct,
                Xselling = Lang.Equals("en") ? listProductItem.XSellingProductEn : listProductItem.XSellingProduct,
                Condition = Lang.Equals("en") ? listProductItem.ConditionProductEn : listProductItem.ConditionProduct,
                ConditionEn = listProductItem.ConditionProductEn,
                ProductNameEn = listProductItem.ProductNameEn,
                OverviewEn = listProductItem.OverviewProductEn,
                DocumentEn = listProductItem.DocumentProductEn,
                XsellingEn = listProductItem.XSellingProductEn,
            };
            response.Record = record;
            return isSuccess;
        }
    }
}
